package com.service;

import com.dao.UserDao;
import com.pojo.Provider;
import com.pojo.User;
import com.utils.JDBCUtils;

import java.sql.Date;
import java.util.List;
import java.util.Objects;

public class UserService {

    private UserDao dao = new UserDao();

    public List<User> queryUserAll() {
        return dao.queryUserAll();
    }

    public boolean addUser(int userCode, String userName, String userpassword, String ruserpassword, boolean gender, Date birthday, String phone, String address, String userRoleName) {
        return dao.AddUser(userCode, userName, userpassword, ruserpassword, gender, birthday, phone, address, userRoleName);
    }

    public User queryById(String id) {
        return (User) dao.QueryById(id).get(0);
    }

    public boolean deleteById(String id) {
        return dao.DeleteById(id);
    }

    public boolean updateUser(String id, String userName, boolean gender, Date birthday, String phone, String userRoleName) {
        boolean updateuser = dao.updateuser(id, userName, gender, birthday, phone, userRoleName);
        return updateuser;
    }

    //select * from user where userName like ? and userRoleName like ?
    public List<User> queryByName(String... args) {

        if (!Objects.equals(args[0], "") && !Objects.equals(args[1], "")) {
//            args[0] = "%"+args[0]+"%";
////            args[1] = "%"+args[1]+"%";
            return dao.queryByName(args[0], args[1]);
        } else {
            if (args[0].equals("")) {
//                args[0] = "%"+args[0]+"%";
                args[0] = "";
                return dao.queryByName(args[0], args[1]);
            } else if (args[1].equals("")) {
//                args[1] = "%"+args[0]+"%";
                args[1] = "";
                return dao.queryByName(args[0], args[1]);
            } else {
                args[0] = "";
                args[1] = "";
                return dao.queryByName(args[0], args[1]);
            }
        }
    }

    public User getUser(String username) {
        return dao.queryOne(username);
    }

    public void pwdModify(String username, String newpassword) {
        dao.updatePwd(username, newpassword);
    }

    public List<User> selectByPage(int pagenum, int pagesize) {
        return dao.selectByPage(pagenum, pagesize);
    }

    public boolean exit(String userName) {
        return dao.queryByUserName(userName).size() > 0;
    }

    public List<User> getUsers(Integer currentPageNo,Integer pageSize) {
        return dao.queryLimit(currentPageNo,pageSize);
    }
}
